package com.ruoyi.web.controller.wms;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.wms.domain.WmsCargoOwner;
import com.ruoyi.wms.service.IWmsCargoOwnerService;
import com.ruoyi.wms.service.IWmsOwnerOrderNumberRuleService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 货主信息Controller
 * 
 * @author ruoyi
 * @date 2024-12-19
 */
@RestController
@RequestMapping("/wms/cargoOwner")
public class WmsCargoOwnerController extends BaseController
{
    @Autowired
    private IWmsCargoOwnerService wmsCargoOwnerService;

    @Autowired
    private IWmsOwnerOrderNumberRuleService wmsOwnerOrderNumberRuleService;

    /**
     * 查询货主信息列表
     */
    @PreAuthorize("@ss.hasPermi('wms:cargoOwner:list')")
    @GetMapping("/list")
    public TableDataInfo list(WmsCargoOwner wmsCargoOwner)
    {
        startPage();
        List<WmsCargoOwner> list = wmsCargoOwnerService.selectWmsCargoOwnerList(wmsCargoOwner);
        return getDataTable(list);
    }

    /**
     * 导出货主信息列表
     */
    @PreAuthorize("@ss.hasPermi('wms:cargoOwner:export')")
    @Log(title = "货主信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WmsCargoOwner wmsCargoOwner)
    {
        List<WmsCargoOwner> list = wmsCargoOwnerService.selectWmsCargoOwnerList(wmsCargoOwner);
        ExcelUtil<WmsCargoOwner> util = new ExcelUtil<WmsCargoOwner>(WmsCargoOwner.class);
        util.exportExcel(response, list, "货主信息数据");
    }

    /**
     * 获取货主信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('wms:cargoOwner:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(wmsCargoOwnerService.selectWmsCargoOwnerById(id));
    }

    /**
     * 新增货主信息
     */
    @PreAuthorize("@ss.hasPermi('wms:cargoOwner:add')")
    @Log(title = "货主信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WmsCargoOwner wmsCargoOwner)
    {
        if (!wmsCargoOwnerService.checkOwnerCodeUnique(wmsCargoOwner))
        {
            return error("新增货主'" + wmsCargoOwner.getOwnerName() + "'失败，货主编码已存在");
        }
        wmsCargoOwner.setCreateBy(getUsername());
        return toAjax(wmsCargoOwnerService.insertWmsCargoOwner(wmsCargoOwner));
    }

    /**
     * 修改货主信息
     */
    @PreAuthorize("@ss.hasPermi('wms:cargoOwner:edit')")
    @Log(title = "货主信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WmsCargoOwner wmsCargoOwner)
    {
        if (!wmsCargoOwnerService.checkOwnerCodeUnique(wmsCargoOwner))
        {
            return error("修改货主'" + wmsCargoOwner.getOwnerName() + "'失败，货主编码已存在");
        }
        wmsCargoOwner.setUpdateBy(getUsername());
        return toAjax(wmsCargoOwnerService.updateWmsCargoOwner(wmsCargoOwner));
    }

    /**
     * 删除货主信息
     */
    @PreAuthorize("@ss.hasPermi('wms:cargoOwner:remove')")
    @Log(title = "货主信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(wmsCargoOwnerService.deleteWmsCargoOwnerByIds(ids));
    }

    /**
     * 获取货主订单编号规则配置列表
     */
    @PreAuthorize("@ss.hasPermi('wms:cargoOwner:query')")
    @GetMapping("/orderNumberRules/{ownerId}")
    public AjaxResult getOrderNumberRules(@PathVariable("ownerId") Long ownerId)
    {
        return success(wmsOwnerOrderNumberRuleService.selectRuleListByOwnerId(ownerId));
    }
}